// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var constant = require("constant");
	var leftMenu = require("../leftMenu/leftMenu");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var css_detail = require("./pickupDetail.css");
	var css_consent = require("./pickupDelayConsent.css");
	var js_platform_main = require("./platform_main.js");
	var valuationCal = require("valuationCal");
	var pickupDelayId;
	var pageIndex = 1,
		pageSize = 5,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var html_oppose_pickup = require("./html_opposePickupApplication.html");
	var init = function(page) {
		pickupDelayId = page.params.pickupDelayId;
		inquirePickupDelayApplicationDetail();
		$("#pickupDelayConsentId").val(pickupDelayId);
		leftMenu.init(6);
		js_platform_main.init(0);
		$(".left").removeClass("none");
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#pickupDelayConsentBtn").unbind("click").click(function() {
			databind.submitDom($("#pickupDelayConsent"), function() {
				base.alert(1, "同意延期还款成功！");
				router.route("main/pickupDelayList.html");
			});
		});
	};

	//提货申请列表
	var inquirePickupDelayApplicationDetail = function() {
		base.fnAjax({
			action: "inquirePickupDelayApplicationDetail.json",
			oData: {
				pickupDelayId: pickupDelayId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);

					var tpl = $("#pickupDelayConsentBulkstockTpl").html();
					var html = juicer(tpl, data);
					$("#pickupDelayConsentBulkstockBody").html(html);
					$("#pickupDelayConsentEstimateAmount").html(data.datas[0].estimateDelayAmount);

					//重新估值
					$("#pickupDelayConsent .valuation_consent").unbind("click").click(function() {
						var oldVal =  $(this).parents("tr").find(".pickupConsentValuation").val();
//						$(this).attr("bulkstockInfoId")
						var unit ="";
						var quantity=0;
						if($(this).attr("measurement")==1){
							unit = "体积：";
							quantity = $(this).attr("volume");
						}else if($(this).attr("measurement")==2){
							unit = "重量：";
							quantity = $(this).attr("weight");
						}
						var conversionfactor = $(this).attr("conversionfactor");
						valuationCal.showCal("重新估值", $(this).attr("bulkstockInfoId"),unit,quantity, $(this).parent().find(".pickupConsentValuation"),$(this).parents("tr").find(".bulkstockPrice"),conversionfactor);
					
						var d = $(this).parents("tr").find(".pickupConsentValuation");
						var f = $(this).parents("tr").find(".pickupConsentDifference");
						var b = $(this).parents("tr").find(".bulkstockValuation");
						setInterval(function() {
							if (oldVal != d.val()) {
								oldVal = d.val();
								f.val(b.html() - d.val()*f.parents("tr").find(".pickupDelayConsentQuantity").html());
								var amount = 0;
								for (var j = 0; j < $(".pickupConsentDifference").length; j++) {
									amount = parseFloat(amount) + parseFloat($($(".pickupConsentDifference")[j]).val());
								}
								$("#pickupDelayDiffAmount").html(amount.toFixed(2));
								$("#pickupDelayAuditAmount").val((parseFloat($("#pickupDelayConsentEstimateAmount").html())*1 + parseFloat($("#pickupDelayDiffAmount").html())).toFixed(2)*1);
							} else {}
						}, 500);
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});